package cn.iocoder.yudao.module.activity.controller.admin;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import cn.iocoder.yudao.module.activity.entity.ActivityDO;
import cn.iocoder.yudao.module.activity.service.ActivityService;
import cn.iocoder.yudao.module.activity.vo.ActivityPageReqVO;
import cn.iocoder.yudao.module.activity.vo.ActivityRespVO;
import cn.iocoder.yudao.module.activity.vo.ActivitySaveReqVO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


@Tag(name = "管理后台 - 招聘活动")
@RestController
@RequestMapping("/activity/")
@Validated
public class ActivityController {

    @Resource
    private ActivityService activityService;

    @PostMapping("/create")
    @Operation(summary = "创建招聘活动")
    //@PreAuthorize("@ss.hasPermission('system:activity:create')")
    public CommonResult<Long> create(@Valid @RequestBody ActivitySaveReqVO createReqVO) {
        return success(activityService.create(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新招聘活动")
    //@PreAuthorize("@ss.hasPermission('system:activity:update')")
    public CommonResult<Boolean> update(@Valid @RequestBody ActivitySaveReqVO updateReqVO) {
        activityService.update(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除招聘活动")
    @Parameter(name = "id", description = "编号", required = true)
    //@PreAuthorize("@ss.hasPermission('system:activity:delete')")
    public CommonResult<Boolean> delete(@RequestParam("id") Long id) {
        activityService.delete(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得招聘活动")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    //@PreAuthorize("@ss.hasPermission('system:activity:query')")
    public CommonResult<ActivityRespVO> get(@RequestParam("id") Long id) {
        ActivityDO activityDO = activityService.get(id);
        return success(BeanUtils.toBean(activityDO, ActivityRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得招聘活动分页")
    //@PreAuthorize("@ss.hasPermission('system:activity:query')")
    public CommonResult<PageResult<ActivityRespVO>> getPage(@Valid ActivityPageReqVO pageReqVO) {
        PageResult<ActivityDO> pageResult = activityService.getPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ActivityRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出招聘活动 Excel")
    //@PreAuthorize("@ss.hasPermission('system:activity:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportExcel(@Valid ActivityPageReqVO pageReqVO, HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ActivityDO> list = activityService.getPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "招聘活动.xls", "数据", ActivityRespVO.class, BeanUtils.toBean(list, ActivityRespVO.class));
    }

}